<?php

class AvaliacaoDao extends Dao {

    /**
     * @var ClienteService
     */
    private $clienteService;

    public function injectDependencies() {
        $this->clienteService = ClienteService::getInstance();
    }

    public function countEvaluationByUser($IDUsuario, $ArPeriod) {
        $Avaliacoes = array('OTIMO' => 0, 'MUITO BOM' => 0, 'REGULAR' => 0, 'PESSIMO' => 0);
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);
        $StSql = TQuery::getSqlFindAllEvaluationsByUser($IDUsuario, $ArPeriod);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $Avaliacao = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
            $Avaliacoes[$Avaliacao->getEnAvaliacao()]++;
        }
        $this->closeConnection();
        $Avaliacoes['TOTAL'] = array_sum($Avaliacoes);
        return $Avaliacoes;
    }

    public function countEvaluationBySector($IDSetor, $ArPeriod) {
        $Avaliacoes = array('OTIMO' => 0, 'MUITO BOM' => 0, 'REGULAR' => 0, 'PESSIMO' => 0);
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);
        $StSql = TQuery::getSqlFindAllEvaluationsBySector($IDSetor, $ArPeriod);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $Avaliacao = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
            $Avaliacoes[$Avaliacao->getEnAvaliacao()]++;
        }
        $this->closeConnection();
        $Avaliacoes['TOTAL'] = array_sum($Avaliacoes);
        return $Avaliacoes;
    }

    public function countEvaluationByCustomer($IDCliente) {
        $Avaliacoes = array('OTIMO' => 0, 'MUITO BOM' => 0, 'REGULAR' => 0, 'PESSIMO' => 0);
        $this->initConnection();
        $this->tMysqli->escape($IDCliente);
        $StSql = TQuery::getSqlFindAllEvaluationsByCustomer($IDCliente);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $Avaliacao = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
            $Avaliacoes[$Avaliacao->getEnAvaliacao()]++;
        }
        $this->closeConnection();
        $Avaliacoes['TOTAL'] = array_sum($Avaliacoes);
        return $Avaliacoes;
    }

    public function countEvaluationByDepartament($IDDepartamento, $ArPeriod) {
        $Avaliacoes = array('OTIMO' => 0, 'MUITO BOM' => 0, 'REGULAR' => 0, 'PESSIMO' => 0);
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);
        $StSql = TQuery::getSqlFindAllEvaluationsByDepartament($IDDepartamento, $ArPeriod);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $Avaliacao = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
            $Avaliacoes[$Avaliacao->getEnAvaliacao()]++;
        }
        $this->closeConnection();
        $Avaliacoes['TOTAL'] = array_sum($Avaliacoes);
        return $Avaliacoes;
    }

    public function findEvaluationsByUser($IDUsuario) {
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);
        $StSql = TQuery::getSqlFindEvaluationByUser($IDUsuario);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArEvaluations = array();
        while($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $ArEvaluations[] = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
        }
        $this->closeConnection();
        return $ArEvaluations;
    }

    public function findEvaluationsBySector($IDSetor) {
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);
        $StSql = TQuery::getSqlFindEvaluationBySector($IDSetor);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArEvaluations = array();
        while($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $ArEvaluations[] = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
        }
        $this->closeConnection();
        return $ArEvaluations;
    }

    public function findEvaluationsByDepartament($IDDepartamento) {
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);
        $StSql = TQuery::getSqlFindEvaluationByDepartament($IDDepartamento);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArEvaluations = array();
        while($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $ArEvaluations[] = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
        }
        $this->closeConnection();
        return $ArEvaluations;
    }

    public function countNumberOfEvaluationsType($EnTipoAvaliacao) {
        $this->initConnection();
        $this->tMysqli->escape($EnTipoAvaliacao);
        $StSql = TQuery::getSqlFindCustomerWithMoreEvaluationType($EnTipoAvaliacao);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArEvaluationTypeReport = array();
        while($ArReportData = mysqli_fetch_assoc($tQuery)) {
            $ArEvaluationTypeReport[] = array(
                'Customer' => $this->clienteService->findById( $ArReportData['IDCliente'] ),
                'NumberOfEvaluations' => $this->countEvaluationByCustomer($ArReportData['IDCliente']),
                'EvaluationTypeNumber' => $ArReportData['NumberOfEvaluations']
            );
        }
        $this->closeConnection();
        return $ArEvaluationTypeReport;
    }

    public function countEvaluationsNumberByCustomer() {
        $this->initConnection();
        $StSql = TQuery::getSqlCountNumberOfEvaluationsByCustomer();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArEvaluationNumberReport = array();
        while($ArReportData = mysqli_fetch_assoc($tQuery)) {
            $ArEvaluationNumberReport[] = array(
                'Customer' => $this->clienteService->findById( $ArReportData['IDCliente'] ),
                'EvaluationNumber' => $ArReportData['NumberOfEvaluations']
            );
        }
        $this->closeConnection();
        return $ArEvaluationNumberReport;
    }

    public function countEvaluationsNumberReport() {

    }

    public function findEvaluationByCalled($IDChamado) {
        $this->initConnection();
        $this->tMysqli->escape($IDChamado);
        $StSql = TQuery::getSqlFindEvaluationByCalled($IDChamado);
        $tQuery = $this->tMysqli->exec($StSql);
        while($ArEvaluationData = mysqli_fetch_assoc($tQuery)) {
            $Evaluation = new Avaliacao($ArEvaluationData['IDAvaliacaoChamado'], $ArEvaluationData['IDChamado'], $ArEvaluationData['EnAvaliacao'], $ArEvaluationData['TxComentario']);
         }
        $this->closeConnection();
        return isset($Evaluation) ? $Evaluation : false;
    }

}

?>
